<?php

namespace App\Jobs;

use Illuminate\Container\Container;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Config;
use Swift_Mailer;
use Swift_SmtpTransport;

class ConfigurableMailable extends Mailable {
	/**
	 * Override Mailable functionality to support per-user mail settings
	 *
	 * @param  \Illuminate\Contracts\Mail\Mailer  $mailer
	 * @return void
	 */
	public function send(Mailer $mailer) {
		$host =  Config::get('mail.host');
		$port =  Config::get('mail.port');
		$security = Config::get('mail.encryption');
		// 配置邮件
		$transport =new Swift_SmtpTransport($host, $port, $security);
		// 注意看，这里是修改为用户邮箱的配置，$this->config是用户传的数据。
		$transport->setUsername($this->config['username']);
		$transport->setPassword($this->config['password']);
		$mailer->setSwiftMailer(new Swift_Mailer($transport));

		// 重启Swift_SmtpTransport中ssl的连接要不然会报错
		$mailer->getSwiftMailer()->getTransport()->stop();

		Container::getInstance()->call([$this, 'build']);

		$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
			$this->buildFrom($message)
				->buildRecipients($message)
				->buildSubject($message)
				->buildAttachments($message)
				->runCallbacks($message);
		});
	}
}